
* Preserve
preserve

* Marginal effects variables
gen rank = _n in 1/5
label define dv 1"{bf:Vote intention for candidate}"
gen dv = 1 in 1/5
label values dv dv
forvalues i = 1/3 {
gen pe_m`i' = .
gen se_m`i' = .	
}

* Generate marginal effects including 95% CIs
forvalues i = 1/3 {
estimates restore m`i'
margins, dydx(treated) at(rank_cand_capped = (1(1)5)) post
replace pe_m`i' = _b[1.treated:1bn._at] if rank == 1 & dv == 1
replace pe_m`i' = _b[1.treated:2._at] if rank == 2 & dv == 1
replace pe_m`i' = _b[1.treated:3._at] if rank == 3 & dv == 1
replace pe_m`i' = _b[1.treated:4._at] if rank == 4 & dv == 1
replace pe_m`i' = _b[1.treated:5._at] if rank == 5 & dv == 1
replace se_m`i' = _se[1.treated:1bn._at] if rank == 1 & dv == 1
replace se_m`i' = _se[1.treated:2._at] if rank == 2 & dv == 1
replace se_m`i' = _se[1.treated:3._at] if rank == 3 & dv == 1
replace se_m`i' = _se[1.treated:4._at] if rank == 4 & dv == 1
replace se_m`i' = _se[1.treated:5._at] if rank == 5 & dv == 1
gen low_m`i' = pe_m`i' - 1.96 * se_m`i'
gen high_m`i' = pe_m`i' + 1.96 * se_m`i'
}

* Graph
gen rank_m1 = rank + 0.15
gen rank_m2 = rank
gen rank_m3 = rank - 0.15

twoway 	(scatter pe_m1 rank_m1, mcolor(midblue) msize(small) msymbol(circle)) ///
		(rspike low_m1 high_m1 rank_m1, lcolor(midblue) lwidth(medthin)) ///
		(scatter pe_m2 rank_m2, mcolor(red) msize(small) msymbol(circle)) ///
		(rspike low_m2 high_m2 rank_m2, lcolor(red) lwidth(medthin)) ///
		(scatter pe_m3 rank_m3, mcolor(midgreen) msize(small) msymbol(circle)) ///
		(rspike low_m3 high_m3 rank_m3, lcolor(midgreen) lwidth(medthin)) ///	
		(connected pe_m1 rank_m1 if rank == 1, mcolor(midblue) msize(small) msymbol(circle) ///
			lcolor(midblue) lwidth(thin)) ///
		(connected pe_m2 rank_m2 if rank == 1, mcolor(red) msize(small) msymbol(circle) ///
			lcolor(red) lwidth(thin)) ///
		(connected pe_m3 rank_m3 if rank == 1, mcolor(midgreen) msize(small) msymbol(circle) ///
			lcolor(midgreen) lwidth(thin)) ///
			, ///
			ytitle("Effect of candidate-level" "versus party-level VAA advice", margin(zero) size(14pt))  ///
			yline(0, lwidth(thin) lpattern(solid) lcolor(black) extend) ///
			ylabel(, angle(horizontal) ///
				grid glcolor(gs10) glpattern(dot) glwidth(thin) gmin gmax)  ///
			xtitle("Candidate-level VAA advice (ranked)", margin(medsmall) size(14pt)) ///
			xscale(reverse range(0.6 5.4) noline) yscale(noline) ///
			xlabel(1 "1{superscript:st}" 2 "2{superscript:nd}" ///
				3 "3{superscript:rd}" 4 "4{superscript:th}" ///
				5 "5{superscript:th}-10{superscript:th}") ///
			legend(order(7 "All candidates" ///
				8 "Participating candidates" ///
				9 "Non-participating candidates") rows(3) size(14pt) ///
				span region(lcolor(none)) bmargin(0 0 0 0) symxsize(*.5) symysize(*.5)) ///
			by(dv, imargin(medium) rows(1) noiyaxes noixaxes ixtitle yrescale note("") ///
				legend(on position(6))) ///
				subtitle(, size(medlarge) color(black) margin(0 0 2 0) ///
				box fcolor(white) lcolor(none) lwidth(none))  ///
			by(, scale(*1.) graphregion(fcolor(white) lcolor(white) lwidth(thin)) bgcolor(white) ///
			plotregion(fcolor(white) lcolor(white))) ///
			graphregion(fcolor(white) lcolor(white) lwidth(thin)) bgcolor(white) ///
			plotregion(fcolor(white) lcolor(black) lwidth(thin)) ///					
			scheme(s2mono) xsize(5) ysize(6) aspect(1)
* Graph edits
gr_edit .b1title.draw_view.setstyle, style(no)
gr_edit .legend.xoffset = 5


* Restore
restore